package com.adtec.yxdemo.mockLogin.service;

import com.adtec.yxdemo.mockLogin.entity.po.YxAccountInfo;
import com.adtec.yxdemo.mockLogin.entity.po.ErrorInfo;
import com.adtec.yxdemo.mockLogin.service.base.BaseErrorInfoService;
import com.adtec.yxdemo.mockLogin.service.base.BaseAccountService;
import com.adtec.yxdemo.utils.LoginUtil;
import com.jfinal.aop.Inject;

import java.util.Date;
import java.util.List;


public class MockLoginService {
    @Inject
    BaseAccountService baseAccountService;
    @Inject
    BaseErrorInfoService baseErrorInfoService;

    /**
     * 模拟登录
     * login_status: 是否可登录
     */
    public void mokeLogin() {
        List<YxAccountInfo> yxAccountInfos = baseAccountService.findAll();
        int start = 0;
        while (start < yxAccountInfos.size()) {
            YxAccountInfo yxAccountInfo = yxAccountInfos.get(start);
            if (yxAccountInfo.getLoginFlag().equals("1") && yxAccountInfo.getLoginHandle().equals("0")) {
                yxAccountInfo.set("login_handle", 1);
                if (LoginUtil.loginByPwd(yxAccountInfo)) {
                    yxAccountInfo.set("login_status", 1);
                    yxAccountInfo.setCookie(null);
                    baseAccountService.update(yxAccountInfo);
                    start++;
                } else {
                    ErrorInfo errorInfo = new ErrorInfo();
                    errorInfo.setAccountNumber(yxAccountInfo.getAccountUsername());
                    errorInfo.setErrorNum(yxAccountInfo.getNum());
                    errorInfo.setErrorMsg(yxAccountInfo.getRemarks());
                    errorInfo.setCreateTime(new Date());
                    baseErrorInfoService.save(errorInfo);
                    start++;
                }
            }
        }
    }
}
